import { Body, Controller, Delete, Get, Post, Put } from '@nestjs/common'
import { AreaService } from './area.service'
@Controller('area')
export class AreaController {
    constructor(private readonly AreaService: AreaService) { }

    @Get()
    get() { 
        return {
            code: 200,
            msg: "获取成功"
        }
    }
 
    @Post('addArea')
    async addArea(@Body() body: any) {
        this.AreaService.addArea(body)
        return {
            code: 200,
            msg: "添加成功"
        }
    }

    @Post("getArea")
    async getArea(@Body() body: any) {
        const { areaName } = body;
        let searchList = [];
        if (areaName) {
            searchList.push({ areaName: { $regex: areaName } });
        }


        let data = await this.AreaService.getArea(searchList)
        let areaNameList= [{
            value: '',
            label: '请选择',
        }]
        let data2 = await this.AreaService.getArea([])

        data2.forEach(item => {
            areaNameList.push({ value: item.areaName, label: item.areaName})
        })
        return {
            code: 200,
            msg: "查询成功",
            data,
            areaNameList
        } 
    } 

    @Put("updateArea") 
    async updateArea(@Body() body: any) {
        this.AreaService.updateArea(body)
        return {
            code: 200,
            msg: "修改成功" 
        }
    } 

    @Delete("deleteArea")
    async deleteArea(@Body() body: any) {
        this.AreaService.deleteArea(body)
        return {
            code: 200,
            msg: "删除成功"
        }
    }

}
